/**
 * 
 */
package ro.utcn.tp.taskManagement.persistence;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import ro.utcn.tp.taskManagement.utils.Config;

/**
 * This class is used to open a hibernate session and to instantiates
 * persistence classes.
 * 
 * @author Coni
 * 
 */
public class PersistenceFactory {
	// private static final Logger logger =
	// Logger.getLogger(PersistenceFactory.class);

	private static String HIBERNATE_CONFIG_FILE = "ro/utcn/tp/taskManagement/persistence/hibernate.cfg.xml";

	private static PersistenceFactory instance = null;

	private static Configuration hibernateConfiguration;

	private static SessionFactory sessionFactory;

	private UsersPersistence usersPersistence;

	private TasksPersistence tasksPersistence;

	private PersistenceFactory() throws Exception {
		this.initSession();
	}

	/**
	 * Creates a session factory object from the config file and initializes the
	 * session
	 * 
	 * @throws PersistenceException
	 */
	private void initSession() throws Exception {
		System.out.println("initSession");
		try {
//			String username = Config.getInstance().getProperty(
//					Config.HIBERNATE_USERNAME_KEY);
//			String password = Config.getInstance().getProperty(
//					Config.HIBERNATE_PASSWORD_KEY);
//			String urlConnection = Config.getInstance().getProperty(
//					Config.HIBERNATE_CONNECTION_URL_KEY);
//			String driver = Config.getInstance().getProperty(
//					Config.HIBERNATE_DRIVER_KEY);
//			String dialect = Config.getInstance().getProperty(
//					Config.HIBERNATE_DIALECT_KEY);

			hibernateConfiguration = new Configuration();
			hibernateConfiguration = hibernateConfiguration
					.configure(HIBERNATE_CONFIG_FILE);

//			hibernateConfiguration.setProperty(
//					Config.HIBERNATE_CONNECTION_URL_KEY, urlConnection);
//			hibernateConfiguration.setProperty(Config.HIBERNATE_USERNAME_KEY,
//					username);
//			hibernateConfiguration.setProperty(Config.HIBERNATE_PASSWORD_KEY,
//					password);
//			hibernateConfiguration.setProperty(Config.HIBERNATE_DRIVER_KEY,
//					driver);
//			hibernateConfiguration.setProperty(Config.HIBERNATE_DIALECT_KEY,
//					dialect);

			sessionFactory = hibernateConfiguration.buildSessionFactory();
			Session session = sessionFactory.openSession();
			session.setFlushMode(FlushMode.COMMIT);
			session.setCacheMode(CacheMode.IGNORE);
		} catch (HibernateException hibernateException) {
			System.out.println(hibernateException.getMessage());
			hibernateException.printStackTrace();
			throw new Exception(hibernateException);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			hibernateConfiguration = null;
			throw new Exception(e);
		}

	}

	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public static PersistenceFactory getInstance() throws Exception {
		if (instance == null) {
			instance = new PersistenceFactory();
		}
		return instance;
	}

	public UsersPersistence getUsersPersistence() {
		if (usersPersistence == null) {
			usersPersistence = new UsersPersistence();
		}
		return usersPersistence;
	}
	public TasksPersistence getTasksPersistence() {
		if (tasksPersistence == null) {
			tasksPersistence = new TasksPersistence();
		}
		return tasksPersistence;
	}
}

